home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / MINHLP.ZIP / MH.C < prev    next >
C/C++ Source or Header  |  1993-09-04  |  40KB  |  1,437 lines

  1. // Printout is in \foo.c
  2. // Still need to create .PRJ file.
  3.  
  4. // Doesn't handle text containing { or }.
  5. // All calls to Parse() need error checking.
  6. // Check header documentation, such as "To compile" note.
  7. // Search for this:
  8. //    puts("XXX Error--Improve this error handling!");
  9. // Search for all XXX occurrences, which represent code to improve.
  10. // Maybe a feature that creates an index or table of contents?
  11.  
  12. // Ask on WINSDK
  13. // - How to specify bold or italic?
  14. // - What's the max length of a line in HC.EXE?
  15. // - How do you create a table of contents?
  16.  
  17. /* ==========================================================
  18.    File:          MH.C -- MiniHelp
  19.    What it is:    Simplifies the creation of help topic files
  20.                   by converting dot commands to RTF.
  21.                   Also eliminates the need for an RTF editor
  22.                   when creating simple help files.
  23.    Compiler:      ANSI C
  24.    By:            Tom Campbell
  25.    Notes:
  26.    To compile:    Link with
  27.  
  28.                   Example using Borland C:
  29.  
  30.                     bcc mh.c
  31.  
  32.                   Example using Microsoft C:
  33.  
  34.                     cl mh.c
  35.  
  36.  
  37.    To test:
  38.  
  39.    To run:
  40.  
  41.    Copyright      1993 by Tom Campbell.  All rights reserved.
  42.    ========================================================== */
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <ctype.h>
  48.  
  49. /* ==========================================================
  50.    TYPE DECLARATIONS
  51.    ========================================================== */
  52.  
  53. typedef int BOOL;
  54. typedef unsigned WORD;
  55.  
  56. /* ==========================================================
  57.    FUNCTION DECLARATIONS
  58.    ========================================================== */
  59.  
  60.   void AddExtension(char *str, char *ext);
  61.    int CreateHelpFromMetascript(char *InFilename,
  62.      char *OutFilename, BOOL MakeTopicKeyword);
  63.   void CreateKeyword(FILE *OutFile, char *Keyword, int Line);
  64.   void CreateTitle(FILE *OutFile, char *Title, int Line);
  65.   void Error(int Line, char *Msg);
  66.   int HasExtension(char *Filename);
  67.   int Parse(char *Input, char *Separators, WORD StartAt,
  68.              BOOL ForceUpper, WORD MaxLen,
  69.              char *Buffer);
  70.   int PossibleLinkInLine(FILE *OutFile, char *NextLine,
  71.     WORD StartAt, int LineNo);
  72.   void Quit(char *Msg, int ErrorLevel);
  73.   void ReplaceExtension(char *Filename, char *Ext);
  74.   int StripExtension(char *input);
  75.   int UpStr(char *Str);
  76.   void WriteChars(FILE *OutFile, char *Line, int Count,
  77.    int LineNo);
  78.   void WriteLine(FILE *OutFile, char *Line, int LineNo);
  79.  
  80. /* ==========================================================
  81.    CONSTANTS
  82.    ========================================================== */
  83.  
  84. #define FALSE 0
  85. #define TRUE (!FALSE)
  86. #define MAX_LINE 500
  87. #define MAX_FILENAME 140
  88.  
  89.  
  90. /* ==========================================================
  91.    AddExtension()
  92.    ========================================================== */
  93.  
  94.   void AddExtension(char *str, char *ext)
  95.  
  96.   /* ==========================================================
  97.      What it does:
  98.        Adds the ext to str if str doesn't already have an extension.
  99.        A period by itself counts as an extension.
  100.        Samples:
  101.          AddExtension("foobar", "bak")  == "foobar.bak"
  102.          AddExtension("combat.", "lib") == "combat."
  103.          AddExtension("vid.obj", ".bak"). == "vid.obj"
  104.  
  105.      Parameters:
  106.        str : Filename w/out extension.
  107.  
  108.        ext : Desired extension.
  109.  
  110.      Returns:
  111.        TRUE on success.
  112.        FALSE on failure.
  113.  
  114.      Before calling:
  115.        The file must be opened with dbfUse().
  116.  
  117.      Standard #include files required:
  118.        string.h
  119.  
  120.      See also:
  121.        HasExtension(), ReplaceExtension()
  122.  
  123.      Example:
  124.        char request[80], response[80];
  125.        printf("\n\nTesting AddExtension().  Please enter a string or \"q\" to quit. ");
  126.        gets(request);
  127.        printf("\nAdd what extension? ");
  128.        gets(response);
  129.        AddExtension(request, response);
  130.        printf("\nResult: \"%s\"", request);
  131.  
  132.  
  133.      Copyright 1992 by Tom Campbell.  All rights reserved.
  134.      ======================================================== */
  135.   {
  136.  
  137.     char *index;
  138.     index = strchr(str, '.');   /* Is there a period in the filename? */
  139.     if (index == NULL) {        /* No. It's safe to add an extension. */
  140.       str = strcat(str, ".");   /* Append a period, */
  141.       str = strcat(str, ext);   /* then then extension. */
  142.     }
  143.     else {                      /* Yes, there was an extension. Write over */
  144.       /* str = str + (index-str); */
  145.       strcpy(++index, ext);
  146.     }
  147.  
  148.   } /* AddExtension() */
  149.  
  150. /* ========================================================== */
  151.    void Error(int Line, char *Msg)
  152. /* ========================================================== */
  153.  
  154. /* ==========================================================
  155.    What it does:
  156.      Displays the line number and the provided error message.
  157.  
  158.      If the line number is 0, doesn't print it (used in a
  159.      case where it's irrelevant, such as not being able to
  160.      create the output file).
  161.  
  162.    Parameters:
  163.      Line : Line number error occurred on.
  164.  
  165.      Msg : Text of error message.
  166.  
  167.    Copyright 1993 by Tom Campbell.  All rights reserved.
  168.    ======================================================== */
  169. {
  170.    if (Error)
  171.      printf("Error on line %d: %s\n", Line, Msg);
  172.    else
  173.      printf("Error: %s\n", Msg);
  174.  
  175. } /* Error() */
  176.  
  177.  
  178. /* ========================================================== */
  179.    int HasExtension(char *Filename)
  180. /* ========================================================== */
  181.  
  182. /* ==========================================================
  183.    What it does:
  184.      Determines whether the specified DOS filename has an
  185.      extension.
  186.  
  187.    Parameters:
  188.      *Filename : File specification, possibly with an
  189.      extension.
  190.  
  191.    Returns:
  192.      Character position in array of extension if present.
  193.      0 if not.
  194.  
  195.    Copyright 1993 by Tom Campbell.  All rights reserved.
  196.    ======================================================== */
  197. {
  198.   int LastPossible, Length;
  199.   Length = strlen(Filename);
  200.   if (Length < 2)
  201.     return 0;
  202.   LastPossible = Length-1;
  203.   while(LastPossible) {
  204.     if (Filename[LastPossible] == '.')
  205.       return LastPossible;
  206.     else
  207.       LastPossible--;
  208.   }
  209.   return 0;
  210. } /* HasExtension() */
  211.  
  212. /* ==========================================================
  213.    Parse()
  214.    ========================================================== */
  215.  
  216.   int Parse(char *Input, char *Separators, WORD StartAt,
  217.              BOOL ForceUpper, WORD MaxLen,
  218.              char *Buffer)
  219.  
  220.   /* ==========================================================
  221.      What it does:
  222.        Copies the next word or token or whatever from Input and
  223.        copies it into Buffer.
  224.  
  225.      Parameters:
  226.        Input : String to be separators into tokens.
  227.  
  228.        Separators : Characters to be used as token delimiters.
  229.  
  230.        StartAt : Index of character at which to start parsing.
  231.  
  232.        ForceUpper : If TRUE, forces the output (which is copied
  233.        to Buffer) to uppercase.
  234.  
  235.        MaxLen : Maximum # of chars to be copied into Buffer.
  236.  
  237.        Buffer : Buffer at least MaxLen chars wide to hold the
  238.        output.
  239.  
  240.      Returns:
  241.        Character position in string after the routine
  242.        was called.
  243.  
  244.        0 if had to return due to error condition, such as a
  245.        separator or input string of length 0.
  246.  
  247.      Standard #include files required:
  248.        string.h
  249.        ctype.h
  250.  
  251.      Example:
  252.        void main(void)
  253.        {
  254.  
  255.          #define MAX_LEN 100
  256.          int EndedAt;
  257.          char Input[MAX_LEN];
  258.          int Len;
  259.          char Separators[MAX_LEN];
  260.          WORD StartAt = 0;
  261.          BOOL ForceUpper = FALSE;
  262.          WORD MaxLen = MAX_LEN;
  263.          #define MAX_TOKEN 5
  264.          char Buffer[MAX_TOKEN + 1];
  265.  
  266.          do {
  267.            Input[0] = '\0';
  268.            printf("String to parse: ");
  269.            gets(Input);
  270.            if (Input[0]) {
  271.              printf("Separators: ");
  272.